"""
Copyright (c) 2022 Guangdong University of Technology
PhotLab is licensed under [Open Source License].
You can use this software according to the terms and conditions of the [Open Source License].
You may obtain a copy of [Open Source License] at: [https://open.source.license/]

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.

See the [Open Source License] for more details.

Author: Meng Xiang, Junjiang Xiang
Created: 2023/8/19
Supported by: National Key Research and Development Program of China
"""


import numpy as np
from scipy import io


def delay(input, delay_time = 0, active = 1, sampling_rate_default = 2e11):
    """信号延时
            Args:
                input: 输入信号
                delay_time: 延时时间
                active: 模块开启
                sampling_rate_default：采样率

            Returns:
                delay: 输出延时后信号
            """

    if active == 0:
        print("delay is not working")
        return input
    if active != 1:
        raise ValueError('active must be 0 or 1!')
    if delay_time < 0:
        raise ValueError('delay_time 要大于0！')

    FreAxisX_Shaping = np.transpose((np.arange(-sampling_rate_default/ 2+sampling_rate_default / len(input),
                                              sampling_rate_default / 2 +sampling_rate_default / len(input), sampling_rate_default / len(input))))
    output_delayed = np.fft.ifft(np.fft.ifftshift(
        (np.fft.fftshift(np.fft.fft(np.squeeze(input), axis=0))) * np.exp(1j * 2 * np.pi * delay_time * FreAxisX_Shaping)))

    return output_delayed





